package com.huike.web.controller.business;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.Page;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.dto.TbBusinessGainDTO;
import com.huike.business.domain.dto.TbBusinessPageDTO;
import com.huike.business.domain.vo.TbBusinessVO;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.TbBusinessService;
import com.huike.common.core.page.PageDTO;
import com.huike.common.result.PageResult;
import com.huike.common.result.Result;
import com.huike.common.utils.SecurityUtils;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商机Controller
 * @date 2023-04-25
 */
/**
 * Business业务控制器
 *
 * @author hgbsb
 * @date 2023/10/15
 */
@Slf4j
@RestController
@RequestMapping("/business")
@RequiredArgsConstructor
public class TbBusinessController {
    private final TbBusinessService businessService;


    /**
     * 获取页面列表
     * @author hgbsb
     * @return {@link Result}<{@link TbBusiness}>
     */
    @GetMapping("/list")
    public PageResult getPageList(TbBusinessPageDTO tbBusinessPageDTO){
        Page<TbBusinessVO> page = businessService.getPageList(tbBusinessPageDTO);
        log.info("==================>"+tbBusinessPageDTO.getParams());
        return PageResult.success(page.getTotal() , page.getResult());
    }

    /**
     * 添加商机业务
     * @author hgbsb
     * @param tbBusiness 结核病业务
     * @return {@link Result}<{@link String}>
     */
    @PostMapping
    public Result<String> addBusiness(@RequestBody TbBusiness tbBusiness){
        businessService.addBusiness(tbBusiness);
        return Result.success("添加成功");
    }

    /**
     * 按 ID 选择
     *
     * @param id 编号
     * @return {@link Result}<{@link TbBusinessVO}>
     */
    @GetMapping("/{id}")
    public Result<TbBusinessVO> selectById(@PathVariable Long id){
         TbBusinessVO tbBusinessVO = businessService.mySelectById(id);
         return Result.success(tbBusinessVO);
    }

    @GetMapping("/pool")
    public PageResult getPoolList(TbBusinessPageDTO tbBusinessPageDTO){
        tbBusinessPageDTO.setStatus(3);
        Page<TbBusinessVO> pageList = businessService.getPageList(tbBusinessPageDTO);
        return PageResult.success(pageList.getTotal() , pageList.getResult());
    }

    @DeleteMapping("/{ids}")
    public Result<String> deleteByIds(@PathVariable List<Long> ids){
        businessService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @PutMapping("/gain")
    public Result<String> gain(@RequestBody TbBusinessGainDTO tbBusinessGainDTO){
        List<TbBusiness> tbBusinesses = businessService.listByIds(Arrays.asList(tbBusinessGainDTO.getIds()));
        tbBusinesses.forEach(tbBusiness -> tbBusiness.setStatus("2"));
        businessService.updateBatchById(tbBusinesses);
        return Result.success("捞取成功");
    }
    
    @PutMapping("/back/{id}/{reason}")
    public Result<String> goToPool(@PathVariable Long id, @PathVariable String reason){
        TbBusiness byId = businessService.getById(id);
        byId.setStatus("3");
        byId.setReasons(reason);
        businessService.updateById(byId);
        return Result.success("踢回成功");
    }

    @PutMapping("/assignment")
    public Result<String> assignment(@RequestBody TbBusinessGainDTO tbBusinessGainDTO){
        businessService.assignment(tbBusinessGainDTO);
        return Result.success("分配成功");
    }
}
